var calendar = {

  	startCalendar: function() {
    	var mon = '一';
		var tue = '二';
		var wed = '三';
		var thur = '四';
		var fri = '五';
		var sat = '六';
		var sund = '日';

		/**
		 * Get current date
		 */
		var d = new Date();
		var strDate = yearNumber + "/" + (d.getMonth() + 1) + "/" + d.getDate();
		var yearNumber = (new Date).getFullYear();
		/**
		 * Get current month and set as '.current-month' in title
		 */
		var monthNumber = d.getMonth() + 1;

		function GetMonthName(monthNumber) {
			var months = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'];
			return months[monthNumber - 1];
		}

		setMonth(monthNumber, mon, tue, wed, thur, fri, sat, sund);

		function setMonth(monthNumber, mon, tue, wed, thur, fri, sat, sund) {
			
			$('.month').text(GetMonthName(monthNumber) + ' ' + yearNumber);
			$('.month').attr('data-month', monthNumber);
			printDateNumber(monthNumber, mon, tue, wed, thur, fri, sat, sund);	
		}
		
		$('.btn-next').on('click', function(e) {
			var monthNumber = $('.month').attr('data-month');
			if (monthNumber > 11) {
				$('.month').attr('data-month', '0');
				var monthNumber = $('.month').attr('data-month');
				yearNumber = yearNumber + 1;
				setMonth(parseInt(monthNumber) + 1, mon, tue, wed, thur, fri, sat, sund);
			} else {
				setMonth(parseInt(monthNumber) + 1, mon, tue, wed, thur, fri, sat, sund);
			};
			
			
		});

		$('.btn-prev').on('click', function(e) {
			var monthNumber = $('.month').attr('data-month');
			if (monthNumber < 2) {
				$('.month').attr('data-month', '13');
				var monthNumber = $('.month').attr('data-month');
				yearNumber = yearNumber - 1;
				setMonth(parseInt(monthNumber) - 1, mon, tue, wed, thur, fri, sat, sund);
			} else {
				setMonth(parseInt(monthNumber) - 1, mon, tue, wed, thur, fri, sat, sund);
			};
			
		});

		/**
		 * Get all dates for current month
		 */

		function printDateNumber(monthNumber, mon, tue, wed, thur, fri, sat, sund) {
			//alert(monthNumber+";"+yearNumber);
			
			init(yearNumber,monthNumber);

			$($('tbody.event-calendar tr')).each(function(index) {
				$(this).empty();
			});

			$($('thead.event-days tr')).each(function(index) {
				$(this).empty();
			});

			function getDaysInMonth(month, year) {
				// Since no month has fewer than 28 days
				var date = new Date(year, month, 1);
				var days = [];
				while (date.getMonth() === month) {
					days.push(new Date(date));
					date.setDate(date.getDate() + 1);
				}
				return days;
			}

			i = 0;

			setDaysInOrder(mon, tue, wed, thur, fri, sat, sund);

			function setDaysInOrder(mon, tue, wed, thur, fri, sat, sund) {
				var monthDay = getDaysInMonth(monthNumber - 1, yearNumber)[0].toString().substring(0, 3);
				if (monthDay === 'Mon') {
					$('thead.event-days tr').append('<td>' + mon + '</td><td>' + tue + '</td><td>' + wed + '</td><td>' + thur + '</td><td>' + fri + '</td><td>' + sat + '</td><td>' + sund + '</td>');
				} else if (monthDay === 'Tue') {
					$('thead.event-days tr').append('<td>' + tue + '</td><td>' + wed + '</td><td>' + thur + '</td><td>' + fri + '</td><td>' + sat + '</td><td>' + sund + '</td><td>' + mon + '</td>');
				} else if (monthDay === 'Wed') {
					$('thead.event-days tr').append('<td>' + wed + '</td><td>' + thur + '</td><td>' + fri + '</td><td>' + sat + '</td><td>' + sund + '</td><td>' + mon + '</td><td>' + tue + '</td>');
				} else if (monthDay === 'Thu') {
					$('thead.event-days tr').append('<td>' + thur + '</td><td>' + fri + '</td><td>' + sat + '</td><td>' + sund + '</td><td>' + mon + '</td><td>' + tue + '</td><td>' + wed + '</td>');
				} else if (monthDay === 'Fri') {
					$('thead.event-days tr').append('<td>' + fri + '</td><td>' + sat + '</td><td>' + sund + '</td><td>' + mon + '</td><td>' + tue + '</td><td>' + wed + '</td><td>' + thur + '</td>');
				} else if (monthDay === 'Sat') {
					$('thead.event-days tr').append('<td>' + sat + '</td><td>' + sund + '</td><td>' + mon + '</td><td>' + tue + '</td><td>' + wed + '</td><td>' + thur + '</td><td>' + fri + '</td>');
				} else if (monthDay === 'Sun') {
					$('thead.event-days tr').append('<td>' + sund + '</td><td>' + mon + '</td><td>' + tue + '</td><td>' + wed + '</td><td>' + thur + '</td><td>' + fri + '</td><td>' + sat + '</td>');
				}
			};
			$(getDaysInMonth(monthNumber - 1, yearNumber)).each(function(index) {
				var index = index + 1;
				if (index < 8) {
					$('tbody.event-calendar tr.1').append('<td date-month="' + monthNumber + '" date-day="' + index + '" date-year="' + yearNumber + '">' + index + '</td>');
				} else if (index < 15) {
					$('tbody.event-calendar tr.2').append('<td date-month="' + monthNumber + '" date-day="' + index + '" date-year="' + yearNumber + '">' + index + '</td>');
				} else if (index < 22) {
					$('tbody.event-calendar tr.3').append('<td date-month="' + monthNumber + '" date-day="' + index + '" date-year="' + yearNumber + '">' + index + '</td>');
				} else if (index < 29) {
					$('tbody.event-calendar tr.4').append('<td date-month="' + monthNumber + '" date-day="' + index + '" date-year="' + yearNumber + '">' + index + '</td>');
				} else if (index < 32) {
					$('tbody.event-calendar tr.5').append('<td date-month="' + monthNumber + '" date-day="' + index + '" date-year="' + yearNumber + '">' + index + '</td>');
				}
				i++;
			});
			
			var date = new Date();
			var month = date.getMonth() + 1;
			var thisyear = new Date().getFullYear();
			setCurrentDay(month, thisyear);
			setEvent();
			displayEvent();

		}

		/**
		 * Get current day and set as '.current-day'
		 */
		function setCurrentDay(month, year) {
			var viewMonth = $('.month').attr('data-month');
			var eventYear = $('.event-days').attr('date-year');
			if (parseInt(year) === yearNumber) {
				if (parseInt(month) === parseInt(viewMonth)) {
					$('tbody.event-calendar td[date-day="' + d.getDate() + '"]').addClass('current-day');
				}
			}
		};

		/**
		 * Add class '.active' on calendar date
		 */
		$('tbody td').on('click', function(e) {
			if ($(this).hasClass('event')) {
				$('tbody.event-calendar td').removeClass('active');
				$(this).addClass('active');
			} else {
				$('tbody.event-calendar td').removeClass('active');
			};
		});

		/**
		 * Add '.event' class to all days that has an event
		 */
		function setEvent(){
			$('.day-event').each(function(i) {
				var eventMonth = $(this).attr('date-month');
				var eventDay = $(this).attr('date-day');
				var eventYear = $(this).attr('date-year');
				var eventClass = $(this).attr('event-class');
				if (eventClass === undefined) eventClass = 'event';
				else eventClass = 'event ' + eventClass;

				if (parseInt(eventYear) === yearNumber) {
					$('tbody.event-calendar tr td[date-month="' + eventMonth + '"][date-day="' + eventDay + '"]').addClass(eventClass);
				}
			});
		};

		/**
		 * Get current day on click in calendar
		 * and find day-event to display
		 */
		 
		function displayEvent() {
			$('tbody.event-calendar td').on('click', function(e) {
				$('.day-event').slideUp('fast');
				var monthEvent = $(this).attr('date-month');
				var dayEvent = $(this).text();
				$('.day-event[date-month="' + monthEvent + '"][date-day="' + dayEvent + '"]').slideDown('fast');
			});
		};

	
		function init(year,month) {
			
			if (year&&month) {
				var day=year+'-'+month+'-'+'01';
				// ajax call to print json
				$.ajax({
		  				url: '../backend/Sign!getPersonalList',
		  				type: 'POST',
						dataType:'json',
						async:false,
						data:{  
							sign_date:day
						}  
		  			})
		  			.done(function(data) {
						//alert(data.data_rows.length);
						//alert(data.data_rows[0].user_id);
						//debugger;
						for (var i = 0; i < data.data_rows.length; i++) {
							//获取年月日
							var sign_date = data.data_rows[i].sign_date;
							var str_year = sign_date.substr(0,4);
							var str_month = sign_date.substr(5,2);
							str_month = parseInt(str_month);
							var str_day = sign_date.substr(8,2);
							str_day = parseInt(str_day);
							$('.list').append('<div class="day-event" date-day="'+ str_day +'" date-month="' + str_month +'" date-year="'+ str_year +'" data-number="'+ i +'"><ul class="record_ul" id="'+ sign_date +'"></ul></div>');
							//获取时间位置
							var id = data.data_rows[i].id;
							var sign_time = data.data_rows[i].sign_time;
							if(sign_time!=null){
								var str_time = sign_time.substr(11,5);
							}
							
							var sign_address = data.data_rows[i].sign_address;
							$('#'+sign_date).append('<li id="' + id + '"><span class="record_ul_sp1">'+ str_time +'</span><span>'+ sign_address +'</span></li>');
						}

						// start calendar
						//calendar.startCalendar();

						//点击详情
						$(".record_ul li").click(function(){
							var id = this.id;
							getAjax_details(id);
							$(".page_one").css("display","none");
							$(".page_two").css("display","block");
						});

		  			})
		  			.fail(function(data) {
		  				/*console.log('calendar');*/
		  				alert("网络请求失败，请重试！");
		  			});

				} else {

			
			}

		}

  	},

};

//ajax请求详情数据
function getAjax_details(id){
	var aj = $.ajax( {  
		url:'../backend/Sign!getSingleRecord',// 跳转到 action  
		data:{  
			id:id
		},  
		type:'post',  
		cache:false,  
		dataType:'json',  
		success:function(data) { 

			$('.showImg img').remove();

			var length = data.childrenData.SignPhotos.length;
			//var obj=eval(data)
			//console.log(data);
			var sign_time = data.sign_time;
			var str_time = sign_time.substr(11,8);
			var sign_address = data.sign_address;
			var distance = data.distance;
			var memo = data.memo;
			var shop = data.shop;
			//打卡时间
			$('.time_catag').text(str_time);
			//是否异常
			if(distance>500){
				$('.yichang').text("打卡异常!");
			}else{
				$('.yichang').text(" ");
			}
			//门店
			$(".company").text(shop);
			//地址
			$('#address').text(sign_address);
			//备注信息
			if(memo!=null){
				$('.reason').text(memo);
			}else{
				$('.reason').text('');
			}
			
			//加载图片
			if(length>0){
				for(var i=0;i<length;i++){
					var url = data.childrenData.SignPhotos[i].url;
					//alert(url);
					$('.showImg').append('<img src="' + url + '" class="image"/>');
				}
			}


		},  
		error : function() {  
			/*alert("异常详情"); */ 
			alert("网络请求失败，请重试！");
		}  
	});
}


$(document).ready(function() {
	//calendar.init('ajax');
	calendar.startCalendar();
});
